package com.spoto.oa.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.spoto.oa.model.NoticeType;
import com.spoto.oa.service.NoticeServ;
import com.spoto.oa.service.impl.NoticeServImpl;

public class NoticeTypeServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public NoticeTypeServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request,response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String type=request.getParameter("op_type");
		NoticeServ noticeServ=new NoticeServImpl();
		NoticeType noticeType=new NoticeType();
		if("to_list".equals(type)){
			List<NoticeType> noticeTypes=noticeServ.getAllNoticeTypes();
			request.setAttribute("noticeTypes", noticeTypes);
			request.getRequestDispatcher("/notice/welcome_noticeType.jsp").forward(request, response);
			
		}else if("delete".equals(type)){//
			int id = Integer.parseInt(request.getParameter("id"));
			noticeServ.deleteTypeById(id);
			List<NoticeType> noticeTypes=noticeServ.getAllNoticeTypes();
			request.setAttribute("noticeTypes", noticeTypes);
			request.getRequestDispatcher("/notice/welcome_noticeType.jsp").forward(request, response);
		}else if ("to_update".equals(type)){//
			int id = Integer.parseInt(request.getParameter("id"));
			
			noticeType = noticeServ.getNoticeTypeById(id);
			request.setAttribute("noticeType", noticeType);
			
				request.getRequestDispatcher("/notice/update_noticeType.jsp")
				.forward(request, response);
		}else if ("update".equals(type)) {
			int id=Integer.parseInt(request.getParameter("id"));

			String name=request.getParameter("name");
			int no=Integer.parseInt(request.getParameter("no"));
			noticeType.setId(id);
			noticeType.setName(name);
			noticeType.setNo(no);
			noticeServ.updateNoticeType(noticeType);
			
			List<NoticeType> noticeTypes=noticeServ.getAllNoticeTypes();
			request.setAttribute("noticeTypes", noticeTypes);
			request.getRequestDispatcher("/notice/welcome_noticeType.jsp").forward(request, response);
		}else if (("multi_del").equals(type)) {//选中删除
			String[] delIds = request.getParameterValues("del-flag");
			for (int i = 0; i < delIds.length; i++) {
				int id=Integer.parseInt(delIds[i]);
				noticeServ.deleteTypeById(id);
			}
			List<NoticeType> noticeTypes=noticeServ.getAllNoticeTypes();
			request.setAttribute("noticeTypes", noticeTypes);
			request.getRequestDispatcher("/notice/welcome_noticeType.jsp").forward(request, response);
		}else if (("add").equals(type)) {//
			String name=request.getParameter("name");
			int no=Integer.parseInt(request.getParameter("no"));
			noticeType.setName(name);
			noticeType.setNo(no);
			noticeServ.addNoticeType(noticeType);
			List<NoticeType> noticeTypes=noticeServ.getAllNoticeTypes();
			request.setAttribute("noticeTypes", noticeTypes);
			request.getRequestDispatcher("/notice/welcome_noticeType.jsp").forward(request, response);
		}
		else if("validate".equals(type)) {  
			String kind=request.getParameter("kind");
			if("name".equals(kind)){
				String name=request.getParameter("name");
				noticeType =noticeServ.getNoticeTypeByName(name);
				PrintWriter out = response.getWriter();
				if(null!=noticeType){
					out.write("false");
				}else{
					out.write("true");
				}
			}else if("no".equals(kind)){
				int no=Integer.parseInt(request.getParameter("no"));
				noticeType =noticeServ.getNoticeTypeByNo(no);
				int oldNo=-1;
				try{
					oldNo=Integer.parseInt(request.getParameter("no_old"));
					
				}catch(Exception e){
					
				}
				PrintWriter out = response.getWriter();
				if(null==noticeType||no==oldNo){
					out.write("true");
				}else{
					out.write("false");
					
				}
			}
			
			
		}
		
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
